package ext;

import java.util.Arrays;

import org.junit.Test;

import util.ArrayGenerator;
import util.TreeNode;
import util.TreeUtils;

public class ParseBst {
    
    public TreeNode parse(int[] nums) {
        Arrays.sort(nums);
        return recur(nums, 0, nums.length - 1);
    }

    private TreeNode recur(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int m = (right - left) / 2 + left;
        TreeNode root = new TreeNode(nums[m]);
        root.left = recur(nums, left, m - 1);
        root.right = recur(nums, m + 1, right);
        return root;
    }

    @Test
    public void test() {
        int[] nums = ArrayGenerator.getArray(100, 10);
        TreeUtils.printBinaryTreeVisualization(parse(nums));
    }

}
